<?php
class Post extends CI_Controller
{
	public function __construct ()
	{
		parent::__construct();
		$this->template->set_template('template/admin_template');
		if($this->general_model->loggedin("admin") == FALSE)
		{
			redirect('admin/users/login');
		}
	}

	public function index ()
	{
		$data['breadcrumb'] = set_crumbs(array(current_url() => 'Posts'));
		
		$data['posts'] = $this->general_model->select_rows('post','*','title','asc','10');
		
		$this->template->content->view('admin/post/posts', $data);
		$this->template->publish();
	}
	public function addpost ()
	{
		$data['breadcrumb'] = set_crumbs(array('Post' => 'Post', current_url() => 'Add Post'));
		$data['author']=$this->session->userdata('user');
		$rulespost = $this->general_model->rules_post;
		$this->form_validation->set_rules($rulespost);
		
		if ($this->form_validation->run() == TRUE)
		{
			$postdata=array(
				'slug'=>$this->input->post('slug'),
				'title'=>$this->input->post('title'),
				'url'=>addslashes($this->input->post('url_path').$this->input->post('url')),
				'content'=>addslashes($this->input->post('content')),
				'status'=>$this->input->post('status'),
				'author_id'=>$this->input->post('author_id'),
				'author'=>$this->input->post('author'),
				'date_added'=>date('Y-m-d H:i:s', strtotime($this->input->post('date_added')))
				
				);
			$pageid=$this->general_model->insert('post',$postdata);
			$this->session->set_flashdata('message', '<p class="success">Post added Saved.</p>');
			if ($this->input->post('save_exit'))
			{
				redirect(ADMIN_PATH . "/post");
			}
			else
			{
				redirect(ADMIN_PATH . "/post/addpost");
			}
		}
		
		// Deteremine where to redirect user
		
		$this->template->content->view('admin/post/addpost', $data);
		$this->template->publish();
	}
	public function editpost()
	{
		$postid=$this->uri->segment(4);
		$data['breadcrumb'] = set_crumbs(array('Post' => 'Post', current_url() => 'Edit Post'));
		$data['author']=$this->session->userdata('adminuser');
		$data['post']=$this->general_model->select_val("post","*","postid = ".$postid);
		$this->template->content->view('admin/post/editpost', $data);
		$this->template->publish();
	}
	public function deletepost()
	{
		if ($this->input->post('selected'))
        {
            $selected = $this->input->post('selected');
        }
        else
        {
            $selected = (array) $this->uri->segment(5);
        }
		$this->general_model->delete_rows('post','postid',$selected);
		$this->session->set_flashdata('message', '<p class="success">Post deleted Successfully.</p>');
		redirect(ADMIN_PATH . "/post");
	}
	
}